<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加新类别</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.all.js"></script>
    <script src="/js/jquery-3.3.1.min.js"></script>
</head>
<body>
<form method="" action="/getCategroy">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">类别名称</label>
            <div class="layui-input-inline">
                <input type="text" id="categoryname" name="categoryname" lay-verify="required|phone" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">类别级别</label>
            <div class="layui-input-inline">
                <input type="text" id="categorylevel" name="categorylevel" lay-verify="email" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">父类ID</label>
            <div class="layui-input-inline">
                <input type="text" id="parentid" name="parentid" lay-verify="required|phone" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">是否显示</label>
            <div class="layui-input-inline">
                <input type="text" id="show" name="show" lay-verify="email" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <input type="text" id="status" name="status" lay-verify="required|phone" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">URL</label>
            <div class="layui-input-inline">
                <input type="text" id="url" name="url" lay-verify="required|phone" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline" style="left: 85px">
            <button type="button" class="layui-btn layui-btn-danger" id="adverpic"><i class="layui-icon"></i>上传广告图</button>
            <div class="layui-inline layui-word-aux">
                限制 3000KB 为例
            </div>
        </div>
    </div>
    <div class="layui-input-block" style="left: 145px">
        <button type="button" class="layui-btn layui-btn-normal" id="tj">确认新增</button>
        <button class="layui-btn layui-btn-warm" id="qx">取消新增</button>
    </div>
</form>
<script>
    layui.use(['form', 'layedit', 'laydate','element','upload'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            ,element=layui.element
            ,upload=layui.upload;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });
        //设定文件大小限制
        upload.render({
            elem: '#adverpic'
            ,url: '#'
            ,size: 3000 //限制文件大小，单位 KB
            ,done: function(res){
                console.log(res)
            }
        });
    });
    var $ = layui.jquery;
    $(function () {
        $("#tj").click(function () {
            var categoryname=$("#categoryname").val();
            if (categoryname.length==0){
                alert("用户名不能为空")
            }else {
                tianjia();
            }
        });
        $("#qx").click(function () {
            parent.location.href="/getProgory";
        })
    });
    function tianjia() {
        var categoryname=$("#categoryname").val();
        var categorylevel=$("#categorylevel").val();
        var parentid=$("#parentid").val();
        var show=$("#show").val();
        var status=$("#status").val();
        var url=$("#url").val();
        var adverpic=$("#adverpic").val();
        $.ajax({
            url:"/addCategroy",
            type:"POST",
            data:JSON.stringify({
                "categoryname":categoryname,
                "categorylevel":categorylevel,
                "parentid":parentid,
                "show":show,
                "status":status,
                "url":url,
                "adverpic":adverpic
            }),
            dataType:'json',
            contentType:'application/json;charset=UTF-8',
            success:function (i) {
                if (i==1){
                    alert("添加成功！");
                    parent.location.href="/getProgory";
                    location.reload();
                }else {
                    alert("添加失败！")
                }
            }
        })

    }

</script>

</body>
</html>